20+ Maven Commands and Options (Cheat Sheet) | 您所在的位置:网站首页 › pom import-package › 20+ Maven Commands and Options (Cheat Sheet) |
Tutorial20+ Maven Commands and Options (Cheat Sheet)Updated on February 10, 2023Maven![]() By Pankaj ![]() Maven is one of the most popular project and dependency management tools for Java applications. Maven provides a lot of commands and options to help you in your day to day tasks. This cheat sheet uses a sample Maven project to demonstrate some useful Maven commands. It was originally written for OpenJDK 13.0.1 and Maven 3.6.3. These commands have been verified with OpenJDK 19.0.1 and Maven 3.8.7. Maven Commands Cheat Sheet![]() This command cleans the Maven project by deleting the target directory: mvn cleanExample of the output: Output[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven-example-jar --- [INFO] Deleting /Users/sammy/Desktop/maven-examples/maven-example-jar/target [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ mvn compiler:compileThis command compiles the Java source classes of the Maven project: mvn compiler:compileExample of the output: Output[INFO] --- maven-compiler-plugin:3.8.1:compile (default-cli) @ maven-example-jar --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to /Users/sammy/Desktop/maven-examples/maven-example-jar/target/classes [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ mvn compiler:testCompileThis command compiles the test classes of the Maven project: mvn compiler:testCompileExample of the output: Output[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-cli) @ maven-example-jar --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to /Users/sammy/Desktop/maven-examples/maven-example-jar/target/test-classes [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ mvn packageThis command builds the Maven project and packages it into a JAR, WAR, etc.: mvn packageExample of the output: Output[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ maven-example-jar --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to /Users/sammy/Desktop/maven-examples/maven-example-jar/target/classes ... [INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ maven-example-jar --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to /Users/sammy/Desktop/maven-examples/maven-example-jar/target/test-classes ... [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-example-jar --- [INFO] Surefire report directory: /Users/sammy/Desktop/maven-examples/maven-example-jar/target/surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running com.example.maven.classes.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-example-jar --- [INFO] Building jar: /Users/sammy/Desktop/maven-examples/maven-example-jar/target/maven-example-jar-0.0.1-SNAPSHOT.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSThe output shows the location of the JAR file just before the "BUILD SUCCESS" message. Notice the package goal executes compile, testCompile, and test goals before packaging the build. mvn installThis command builds the Maven project and installs the project files (JAR, WAR, pom.xml, etc.) to the local repository: mvn installExample of the output: Output[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven-example-jar --- ... [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ maven-example-jar --- ... [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven-example-jar --- ... [INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ maven-example-jar --- ... [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven-example-jar --- ... [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ maven-example-jar --- ... [INFO] --- maven-install-plugin:2.4:install (default-install) @ maven-example-jar --- [INFO] Installing /Users/sammy/Desktop/maven-examples/maven-example-jar/target/maven-example-jar-0.0.1-SNAPSHOT.jar to /Users/sammy/.m2/repository/com/example/maven/maven-example-jar/0.0.1-SNAPSHOT/maven-example-jar-0.0.1-SNAPSHOT.jar [INFO] Installing /Users/sammy/Desktop/maven-examples/maven-example-jar/pom.xml to /Users/sammy/.m2/repository/com/example/maven/maven-example-jar/0.0.1-SNAPSHOT/maven-example-jar-0.0.1-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS mvn deployThis command deploys the artifact to the remote repository: mvn deployThe remote repository should be configured properly in the project pom.xml file distributionManagement tag. The server entries in the Maven settings.xml file are used to provide authentication details. mvn validateThis command validates the Maven project to ensure that everything is correct and all the necessary information is available: mvn validate mvn dependency:treeThis command generates the dependency tree of the Maven project: mvn dependency:treeExample of the output: Output[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ Mockito-Examples --- [INFO] com.example.mockito:Mockito-Examples:jar:1.0-SNAPSHOT [INFO] +- org.junit.platform:junit-platform-runner:jar:1.2.0:test [INFO] | +- org.apiguardian:apiguardian-api:jar:1.0.0:test [INFO] | +- org.junit.platform:junit-platform-launcher:jar:1.2.0:test [INFO] | \- org.junit.platform:junit-platform-suite-api:jar:1.2.0:test [INFO] | \- org.junit.platform:junit-platform-commons:jar:1.2.0:test [INFO] +- org.junit.jupiter:junit-jupiter-engine:jar:5.2.0:test [INFO] | +- org.junit.platform:junit-platform-engine:jar:1.2.0:test [INFO] | | \- org.opentest4j:opentest4j:jar:1.1.0:test [INFO] | \- org.junit.jupiter:junit-jupiter-api:jar:5.2.0:test [INFO] +- org.mockito:mockito-junit-jupiter:jar:2.19.0:test [INFO] | \- org.mockito:mockito-core:jar:2.19.0:test [INFO] | +- net.bytebuddy:byte-buddy:jar:1.8.10:test [INFO] | +- net.bytebuddy:byte-buddy-agent:jar:1.8.10:test [INFO] | \- org.objenesis:objenesis:jar:2.6:test [INFO] \- org.testng:testng:jar:6.14.3:test [INFO] +- com.beust:jcommander:jar:1.72:test [INFO] \- org.apache-extras.beanshell:bsh:jar:2.0b6:test mvn dependency:analyzeThis command analyzes the maven project to identify the unused declared and used undeclared dependencies: mvn dependency:analyzeExample of the output: Output[INFO] --- maven-dependency-plugin:2.8:analyze (default-cli) @ Mockito-Examples --- [WARNING] Used undeclared dependencies found: [WARNING] org.junit.jupiter:junit-jupiter-api:jar:5.2.0:test [WARNING] org.mockito:mockito-core:jar:2.19.0:test [WARNING] Unused declared dependencies found: [WARNING] org.junit.platform:junit-platform-runner:jar:1.2.0:test [WARNING] org.junit.jupiter:junit-jupiter-engine:jar:5.2.0:test [WARNING] org.mockito:mockito-junit-jupiter:jar:2.19.0:testIt’s useful in reducing the build size by identifying the unused dependencies and removing them from the pom.xml file. mvn archetype:generateThis command generates skeleton Maven projects of different types, such as JAR, web application, Maven site, etc: mvn archetype:generateExample of the output: Output[INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Archetype: maven-archetype-quickstart:1.4 [INFO] ---------------------------------------------------------------------------- ... [INFO] Project created from Archetype in dir: /Users/sammy/Desktop/maven-examples/maven-example-jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSRecommended Reading: Creating a Java Project using Maven Archetypes mvn site:siteThis command generates a site for the project: mvn site:siteYou will notice a site directory in the target directory after executing this command. /Users/sammy/Desktop/maven-examples/maven-example-jar/target/site/index.htmlThere will be multiple HTML files inside the site directory that provide information related to the project. mvn testThis command runs the test cases of the project: mvn testExample of the output: Output[INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running TestSuite first-element second-element Employee setName Argument = Sammy ... [INFO] Results: [INFO] [INFO] Tests run: 5, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS mvn compileThis command compiles the source Java classes of the project: mvn compileExample of the output: Output[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ Mockito-Examples --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent! [INFO] Compiling 10 source files to /Users/sammy/Desktop/maven-examples/Mockito-Examples/target/classesIt is similar to the previous mvn compiler:compile command, but runs the entire Maven lifecycle up to compile. mvn verifyThis command builds the project, runs all the test cases and run any checks on the results of the integration tests to ensure quality criteria are met: mvn verify Maven OptionsMaven provides a lot of command-line options to alter the Maven build process: mvn -helpThis command-line option prints the Maven usage and all the available options: mvn -helpExample of the output: Outputusage: mvn [options] [] [] Options: -am,--also-make If project list is specified, also build projects required by the list -amd,--also-make-dependents If project list is specified, also build projects that depend on projects on the list -B,--batch-mode Run in non-interactive (batch) mode (disables output color) -b,--builder The id of the build strategy to use -C,--strict-checksums Fail the build if checksums don't match -c,--lax-checksums Warn if checksums don't match -cpu,--check-plugin-updates Ineffective, only kept for backward compatibility mvn -f dir/pom.xml packageThis command-line option builds a project from a different location: mvn -f dir/pom.xml packageProvide the pom.xml file location to build the project. It’s useful when you have to run a Maven build from a script. mvn -o packageThis command-line option runs the Maven build in offline mode: mvn -o packageIt’s useful when you have all the required JARs downloaded in the local repository and you don’t want Maven to look for any JARs in the remote repository. mvn -q packageThis command-line option runs the Maven build in quiet mode, so that only the test case results and errors are displayed: mvn -q package mvn -X packageThis command-line option prints the Maven version and runs the build in debug mode, so that all messages are displayed: mvn -X packageExample of the output: OutputApache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f) Maven home: /Users/sammy/Downloads/apache-maven-3.6.3 Java version: 13.0.1, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home Default locale: en_IN, platform encoding: UTF-8 OS name: "mac os x", version: "10.15.1", arch: "x86_64", family: "mac" [DEBUG] Created new class realm maven.api [DEBUG] Importing foreign pacakges into class realm maven.api [DEBUG] Imported: javax.annotation.* < plexus.core [DEBUG] Imported: javax.annotation.security.* < plexus.core [DEBUG] Imported: javax.enterprise.inject.* < plexus.core [DEBUG] Imported: javax.enterprise.util.* < plexus.core [DEBUG] Imported: javax.inject.* < plexus.core mvn -vThis command-line option displays the Maven version information: mvn -vExample of the output: OutputApache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f) Maven home: /Users/sammy/Downloads/apache-maven-3.6.3 Java version: 13.0.1, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk-13.0.1.jdk/Contents/Home Default locale: en_IN, platform encoding: UTF-8 OS name: "mac os x", version: "10.15.1", arch: "x86_64", family: "mac" mvn -V packageThis command-line option prints the Maven version and then continues with the build: mvn -V packageIt’s equivalent to the commands: mvn -v;mvn package mvn -DskipTests packageThis command-line option applies the skipTests system property to skip the unit test cases from the build cycle: mvn -DskipTests packageYou can also skip the test cases execution: mvn -Dmaven.test.skip=true package mvn -T 4 clean installThis command-line option tells Maven to run parallel builds using the specified thread count: mvn -T 4 clean installIt’s useful in multiple module projects where modules can be built in parallel. It can reduce the build time of the project. References Maven Plugins Maven CLI Options ReferenceThanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases. Learn more about us About the authors![]() author ![]() editor Still looking for an answer?Ask a questionSearch for more helpWas this helpful? JournalDevDigitalOcean EmployeeHello! Excellent summary :) There is one option that should be added because of its usefulness: mvn help:effective-settings With it, you can query the settings Maven is detecting from your config, so it’s very handy when there are problems with some of them being not detected because bad writing or so - Fer B JournalDevDigitalOcean Employeehow do I terminate mvn? - Alexander Kappelhoff JournalDevDigitalOcean Employeecan you please explain -U role in mvn command - jack ![]() |
CopyRight 2018-2019 实验室设备网 版权所有 |